<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket客户端</title>
</head>

<body>
    <div>
        <input type="text" id="text">
        <button id="btn">点击</button>
    </div>
    <script>
        const input = document.querySelector("#text");
        const btn = document.querySelector("#btn")

        const ws = new WebSocket("ws://localhost:8888/ws");

        btn.addEventListener("click",function(){
            const value = input.value;
            console.log("客户端准备发送数据：",value);
            if (value) {
                ws.send(value);
            }
        });

        

        // ws.onopen = function (evt) {
        //     console.log("Connection open ...");
        //     ws.send("第一次连接");
        //     // setInterval(() => {
        //     //     ws.send("Hello WebSockets!");
        //     // },1000)
            
        // };

        ws.onmessage = function (evt) {
            console.log("客户端收到服务端数据: " + evt.data);
            // ws.close();
        };

        ws.onclose = function (evt) {
            console.log("Connection closed.",evt);
        };

    </script>
</body>

</html>